package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbzMaterialList;
import com.jy.adscm.operation.mscyb.bean.JqbzMaterialListCheck;
import com.jy.adscm.operation.mscyb.bean.JqzzMaterialList;
import com.jy.adscm.operation.mscyb.bean.JqzzMaterialListCheck;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzMaterialListCheckMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzMaterialListMapper;
import com.jy.adscm.operation.mscyb.service.JqzzMaterialListCheckService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.service.JqzzMaterialListService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-02
 */
@Service
public class JqzzMaterialListCheckServiceImpl extends ServiceImpl<JqzzMaterialListCheckMapper, JqzzMaterialListCheck> implements JqzzMaterialListCheckService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    JqzzMaterialListMapper jqzzMaterialListMapper;
     @Autowired
     JqzzMaterialListService jqzzMaterialListService;
    @Override
    public boolean updatejqzzMaterialListCheck(List<JqzzMaterialListCheck> jqzzMaterialListChecks , Token token) throws AdscmSystemException  {

        for (JqzzMaterialListCheck materialListCheck : jqzzMaterialListChecks) {
            if ("结果".equals(materialListCheck.getName())){
                //查询到检查的物料
                JqzzMaterialList jqzzMaterialList = jqzzMaterialListMapper.selectById(materialListCheck.getPid());
                if ("1".equals(materialListCheck.getValue())){
                    jqzzMaterialList.setState(1);
                }
                if ("0".equals(materialListCheck.getValue())){
                    jqzzMaterialList.setState(0);
                }
                //修改这个物料
                jqzzMaterialList.setCreator(token.getUser().getId().toString());
                jqzzMaterialList.setCreateTime(new Date());
                jqzzMaterialListService.updateJqzzMaterrialList(jqzzMaterialList);
            }
            boolean b = super.updateById(materialListCheck);
        }




        return true;
    }
    @Override
    public List<JqzzMaterialListCheck> selectByPid(String pid) throws AdscmSystemException {

        Wrapper<JqzzMaterialListCheck> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        wrapper.orderBy("rank",false);
        List<JqzzMaterialListCheck> jqzzMaterialListChecks = super.selectList(wrapper);
        for (JqzzMaterialListCheck jqzzMaterialListCheck : jqzzMaterialListChecks) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMaterialListCheck.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqzzMaterialListCheck.getCreator());
                jqzzMaterialListCheck.setCreatorName(jyUser.getName());
            }
        }
        return jqzzMaterialListChecks;
    }
}
